#include"../mqserver/mq_exchange.hpp"
#include<gtest/gtest.h>

xiang11mq::ExchangeManager::ptr emp;

class ExchangeTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        std::cout << "单元测试执行前的环境初始化" << std::endl;
        emp = std::make_shared<xiang11mq::ExchangeManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        std::cout << "单元测试执行完毕后的环境清理" << std::endl;
        //emp->clear();
    }
};

// TEST(exchange_test, insert_test)
// {
//     std::unordered_map<std::string, std::string> map = {{"k1", "v1"}, {"k2", "v2"}};
//     emp->declareExchange("exchange1", xiang11mq::ExchangeType::DIRECT, true, false, map);
//     emp->declareExchange("exchange2", xiang11mq::ExchangeType::DIRECT, true, false, map);
//     emp->declareExchange("exchange3", xiang11mq::ExchangeType::DIRECT, true, false, map);
//     emp->declareExchange("exchange4", xiang11mq::ExchangeType::DIRECT, true, false, map);
//     ASSERT_EQ(emp->size(), 4);
// }

TEST(exchange_test, select_test) 
{
    ASSERT_EQ(emp->exists("exchange3"), false);
    ASSERT_EQ(emp->exists("exchange1"), true);
    ASSERT_EQ(emp->exists("exchange2"), true);
    ASSERT_EQ(emp->exists("exchange4"), true);
    xiang11mq::Exchange::ptr exp = emp->selectExchange("exchange2");
    ASSERT_EQ(exp->name, "exchange2");
    ASSERT_EQ(exp->durable, true);
    ASSERT_EQ(exp->auto_delete, false);
    ASSERT_EQ(exp->type, xiang11mq::ExchangeType::DIRECT);
    ASSERT_EQ(exp->getArgs(), std::string("k1=v1&k2=v2&"));
}

TEST(exchange_test, delete_test) 
{
    emp->deleteExchange("exchange2");
    //emp->deleteExchange("exchange10");
    xiang11mq::Exchange::ptr exp = emp->selectExchange("exchange2");
    ASSERT_EQ(exp.get(), nullptr);
    ASSERT_EQ(emp->exists("exchange2"), false);
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);

    testing::AddGlobalTestEnvironment(new ExchangeTest);
    //testing::AddGlobalTestEnvironment(new MyMapTest);
    int ret = RUN_ALL_TESTS();
    return 0;
}